$(function () {
    //常用jQuery对象
    var entity_datagrid = $("#entity_list");
    var entity_dailog = $("#entity_dialog");
    var entity_form = $("#entity_form");


    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            $("#entity_form input").prop("disabled", false);
            $("#entity_dialog_btns").show();
            entity_form.form("clear");
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "学校联系人新增");
        },

        //编辑按钮方法
        edit: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //回显

            //回显大学,先判断大学是否为空
            if (row.university) {
                row["university.id"] = row.university.id;
            }


            entity_form.form("clear");
            $(".easyui-combobox").combobox({
                disabled: false
            });
            $(".easyui-datebox").datebox({
                disabled: false
            });
            $("#entity_form input").prop("disabled", false);

            $("#entity_dialog_btns").show();

            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            //回显列表数据
            entity_dailog.form("load", row);

        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_form.form("submit", {
                url: "/contact/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                            entity_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        delete: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示', "请问确定要删除此角色吗?", function (r) {
                if (r) {
                    $.get("/contact/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },


        //高级查询
        searchs: function () {
            var keyword = $("#keyword").textbox("getValue");
            var uId = $("#uId").combobox("getValue");
            entity_datagrid.datagrid("load", {
                keyword: keyword,
                uId: uId,
            });
        },

        //刷新页面
        reloadList: function () {
            entity_datagrid.datagrid("reload");
        },

        //查看
        see: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }

            // /回显
            //回显大学,先判断大学是否为空
            if (row.university) {
                row["university.id"] = row.university.id;
            }


            $("#entity_form input").prop("disabled", true);
            $(".easyui-combobox").combobox({
                disabled: true
            });
            $(".easyui-datebox").datebox({
                disabled: true
            });
            $("#entity_dialog_btns").hide();


            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");


            //回显列表数据
            entity_dailog.form("load", row);

        },
        cancel: function () {
            entity_dailog.dialog("close");
        },
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    entity_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/contact/query.do",
        pagination: true,
        singleSelect: true,
        nowrap: false,
        toolbar: "#tools",
        columns: [[
            {field: 'id', title: '编号', width: 50, align: 'center'},
            {field: 'name', title: '姓名', width: 50, align: 'center'},
            {
                field: 'gander', title: '性别', width: 30, align: 'center', formatter: function (value, row, index) {
                    if (value == null) {
                        return "";
                    } else {
                        return value ? "男" : "女";
                    }
                }
            },
            {field: 'birthday', title: '生日', width: 50, align: 'center'},
            {
                field: 'university',
                title: '所属学校',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {field: 'dept', title: '部门', width: 50, align: 'center'},
            {field: 'duty', title: '职务', width: 50, align: 'center'},
            {field: 'tel', title: '电话', width: 50, align: 'center'},
            {field: 'qq', title: 'QQ', width: 50, align: 'center'},
            {field: 'email', title: '邮箱', width: 60, align: 'center'},
            {field: 'intro', title: '简介', width: 70, align: 'center'},
        ]],


    });

    //编辑添加弹出框设置
    entity_dailog.dialog({
        width: 400,
        height: 400,
        closed: true,
        buttons: "#entity_dialog_btns",
        onClose: function () {
            $(".easyui-combobox").combobox({
                disabled: false
            });
            $(".easyui-datebox").datebox({
                disabled: false
            });
            $("#entity_form input").prop("disabled", false);
        }
    });


    //form表单设置
    entity_dailog.form({
        fit: true
    });

});

